﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using CodeGen.TS.Generation.Members;

namespace CodeGen.TS.Generation.Generators.MVC
{
    public class ApiController : ICodeGenerator
    {
        ICodeGenerator baseClassGenerator;

        const string ApiControllerFullqualified = "System.Web.Http.ApiController";

        //Class is meant to easily generate TypeScript Code from an Asp.net MCV 4 API Controller. It is not finished yet (obviously)

        public BaseTypeMode BaseTypeMode
        {
            get;
            set;
        }
        public ApiController() { this.BaseIntend =  Properties.Settings.Default.IndentString; }
        public ApiController(Type tp)
        {
            this.Type = tp;
        }

        public int RecursionLevel
        {
            get;
            set;
        }

        public string OutputFileName
        {
            get;
            set;
        }

        public bool UseObservable
        {
            get;
            set;
        }

        private Type type=null;
        public Type Type
        {
            get { return type; }
            set
            {
                if (type != value)
                {
                    type = value;
                    CheckBaseClass();
                }
            }
        }

        public string BaseIntend
        {
            get;
            set;
        }

        private string newTypeName;
        public string NewTypeName
        {
            get
            {
                return newTypeName;
            }
            set
            {
                newTypeName = value;
            }
        }

        public string ModulName
        {
            get;
            set;
        }

        private void CheckBaseClass()
        {
            if (DerivesBaseClass())
            {
                Logger.LogWarning("ApiController {0} does not derive from {1}", this.Type.Name, ApiControllerFullqualified);
            }
        }

        private bool DerivesBaseClass()
        {
            var typeToCheck = Type;
            while (typeToCheck != null)
            {
                if (typeToCheck.FullName == ApiControllerFullqualified)
                    return true;

                typeToCheck = typeToCheck.BaseType;
            }
            return false;
        }


        private IList<TSMethodInfo> MethodInfos { get; set; }

        public string GenerateOutput()
        {
            string extendsImplementsString = "";
            if (baseClassGenerator != null)
            {
                extendsImplementsString = "extends " + (baseClassGenerator.ModulName == null || baseClassGenerator.ModulName == this.ModulName ? "" : baseClassGenerator.ModulName + ".") + baseClassGenerator.NewTypeName;
            }
            string returnString = BaseIntend + (ModulName == null ? "" : "export ") + "class " + NewTypeName + " " + extendsImplementsString + "{";
            
            returnString += GenerateMethods(this.MethodInfos, this.UseObservable, BaseIntend);
            returnString += Environment.NewLine + BaseIntend + "}";
            return returnString;
        }

        string GenerateMethods(IEnumerable<TSMethodInfo> methods, bool useObservable, string intend)
        {
            string returnString = "";
            foreach (var method in methods)
            {

                returnString += Environment.NewLine  + method.GetDefinition(intend + Properties.Settings.Default.IndentString);
            }
            return returnString;
        }




        public bool IsPreparedForOutput
        {
            get;
            private set;
        }

        public void PrepareForOutput(bool allowRecursion)
        {
            if (NewTypeName == null && type != null)
                NewTypeName = type.Name;
            this.MethodInfos = CodeGenerationHelper.GetTSMethods(this, allowRecursion);
            if (BaseTypeMode == TS.BaseTypeMode.Exclude && type.BaseType != null && type.BaseType != typeof(object) && allowRecursion)
            {
                this.baseClassGenerator = Output.TypeGeneratorFactory.GetGenerator(type.BaseType, this.UseObservable);
                if (this.baseClassGenerator == null)
                {
                    this.baseClassGenerator = new ClassGenerator(type.BaseType);
                    this.CopyToDependend(this.baseClassGenerator);
                    if (this.type.Namespace == this.type.BaseType.Namespace)
                    {
                        this.baseClassGenerator.ModulName = this.ModulName;
                    }
                    else
                    {
                        this.baseClassGenerator.ModulName = this.type.BaseType.Namespace;
                    }
                    Output.TypeGeneratorFactory.RegisterGenerator(this.baseClassGenerator);
                }

            }
            IsPreparedForOutput = true;
        }

       
    }
}
